<?php
class m_pengeluaran extends Model{
    function m_pengeluaran(){
        parent::Model();
    }
    function addPengeluaran($data){
        $id_user = $this->session->userdata('id_member');
        
        $i = 0;
        foreach ($data['tanggal'] as $row){
           if($row != ''){  
             $insert = $this->db->query("insert into pengeluaran (id_petugas,tanggal,jumlah,keterangan,status) values ('$id_user','".date2mysql($row)."','".currencyToNumber($data['jumlah'][$i])."','".$data['keterangan'][$i]."','0')");   
           }
           $i++;
        }
        
        return TRUE;
    }
    function getPengeluaran($bulan = NULL,$tahun = NULL){
        $tanggal = betweenDate($bulan, $tahun);
        $query = $this->db->query("select p.*,m.nama from pengeluaran p join member m on p.id_petugas = m.id where p.tanggal between '$tanggal[awal]' and '$tanggal[akhir]'");
        
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
    function unapprovePengeluaran($id,$bulan,$tahun,$jumlah){
        $updateRekap = $this->db->query("update rekap_keuangan_bulanan set jumlah = (jumlah+$jumlah) where id = '".$bulan.$tahun."'"); 
        $query = $this->db->query("update pengeluaran set status = '0' where id = '$id'");
        if($query) return TRUE;
    }
    function getCountRows($params){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where tanggal between '$params[awal]' and '$params[akhir]'";
        }
        $query = $this->db->query("select * from pengeluaran $where");
        
        return $query->num_rows();
    }
    function getListPengeluaran($offset = NULL,$limit = NULL,$params = NULL){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where p.tanggal between '$params[awal]' and '$params[akhir]'";
        }
        if($offset != NULL || $limit != NULL){
            $limits = "limit $offset,$limit";
        }else $limits = '';
        $query = $this->db->query("select p.*,m.nama as petugas from pengeluaran p join member m on p.id_petugas = m.id $where $limits");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
}
?>
